package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.programme.ParameterInfoRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@CacheConfig(cacheNames = "projectCache")
public class ProjectService {
    @Autowired
    private ParameterInfoRepository parameterInfoRepository;

    /**
     * 分页 根据创建时间排序
     *
     * @param pageNum
     * @return
     */
    @Cacheable(value = "projectPage")
    public Page<ParameterInfo> findAllParameterInfos(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ParameterInfo> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<ParameterInfo> page = parameterInfoRepository.findAll(mySpecifications, pageable);
        return page;
    }
    // 保存
    @CacheEvict(value = "projectPage", allEntries = true)
    public void saveParameterInfo(String id, String code, String name, Integer objName, Integer objType, Integer propertyType, String unit,
                                  Integer source, Integer isShow) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        ParameterInfo parameterInfo = new ParameterInfo();
        String uuid = "";
        if (id != null) {
        } else {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            parameterInfo.setId(uuid);
            parameterInfo.setCode(code);
            parameterInfo.setName(name);
            parameterInfo.setObjName(objName);
            parameterInfo.setObjType(objType);
            parameterInfo.setPropertyType(propertyType);
            parameterInfo.setUnit(unit);
            parameterInfo.setSource(source);
            parameterInfo.setIsShow(isShow);
            parameterInfo.setCreateTime(date);
            parameterInfo.setCreateUser(new User(shiroUser.getId()));
            parameterInfo.setIsValid(1);
        }
        parameterInfoRepository.save(parameterInfo);
    }
    //参数测点删除
    public void delParameterInfo(String[] ids) {
        List<ParameterInfo> parameterInfos = parameterInfoRepository.findAll(Arrays.asList(ids));

        parameterInfoRepository.delete(parameterInfos);
    }
    //参数测点编辑框回显数据
    public ParameterInfo findById(String id) {
        ParameterInfo code = parameterInfoRepository.findOne(id);
        return code;
    }
}
